package yinghan;

import java.awt.*;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.JTextComponent;

import com.mysql.jdbc.PreparedStatement;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

public class FY extends JFrame {

	private JPanel contentPane;
	private JTextField input;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
				FY frame = new FY();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FY() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 947, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		input = new JTextField();
		input.setBounds(94, 18, 304, 85);
		contentPane.add(input);
		input.setColumns(10);

		JTextArea output = new JTextArea();
		output.setBounds(94, 113, 304, 120);
		contentPane.add(output);

		JButton search = new JButton("search");
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String str;
					String english, china;
					String re = input.getText();
					char ch[] = re.toCharArray();
					String regex = "[A-Za-z]+";
					String regex1 = "[\\u4e00-\\u9fa5]+";
					 PreparedStatement pre ;
					Class.forName("com.mysql.jdbc.Driver");
					// System.out.println("ɹ");
					Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/list", "root", "");
					// System.out.println("Mysqlɹ");
					if (re.matches(regex)) {
						str = "select * from mess where english=?";
						pre =  (PreparedStatement) con.prepareStatement(str);
						pre.setString(1, re);
						ResultSet rs = pre.executeQuery();
						while (rs.next()) {
							output.setText(rs.getString(2));
						}
				} else if (re.matches(regex1)) {
						str = "select * from mess where china=?";
						pre =  (PreparedStatement) con.prepareStatement(str);
						pre.setString(1, re);
					    ResultSet rs = pre.executeQuery();
						while (rs.next()) {
						output.setText(rs.getString(1));
						}

					}
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		search.setBounds(489, 33, 93, 54);
		contentPane.add(search);
	}
}